<template>
	<view class="content">
		<view class="bgc">
			<view class="avatar-box">
				<view>
					<avatar selWidth="300upx" selHeight="300upx" @upload="myUpload" :avatarSrc="avatar"
						:canRotate="false" avatarStyle="width: 200upx; height: 200upx; border-radius: 100%;">
					</avatar>
					<!-- <u-avatar :src="data.avatar" size="160"></u-avatar> -->
				</view>
				<view class="nickname">{{data.ipname}}</view>
			</view>
			<view class="cell-warp">
				<view @click="jumpEdit(0)" style="border-bottom: 1px solid #ebebeb;">
					<listCell leftText="昵称" rightIcon="../../../static/icon/my_module/healthInfo/jiantou.png"
						:rightText="data.ipname"></listCell>
				</view>
				<view @click="jumpEdit(1)" style="border-bottom: 1px solid #ebebeb;">
					<listCell leftText="姓名" rightIcon="../../../static/icon/my_module/healthInfo/jiantou.png"
						:rightText="data.name"></listCell>
				</view>
				<view @click="jumpEdit(2)">
					<listCell leftText="性别" rightIcon="../../../static/icon/my_module/healthInfo/jiantou.png"
						:rightText="data.gender">
					</listCell>
				</view>
		<!-- 		<view @click="jumpEdit(3)" style="border-bottom: 1px solid #ebebeb;">
					<listCell leftText="所在地" rightIcon="../../../static/icon/my_module/healthInfo/jiantou.png"
						:rightText="data.address"></listCell>
				</view> -->
			</view>
			<view class="cell-warp">
				<view style="border-bottom: 1px solid #ebebeb;">
					<listCell leftText="手机号" rightIcon="../../../static/icon/my_module/healthInfo/jiantou.png"
						:rightText="data.phone"></listCell>
				</view>
				<listCell leftText="第三方应用" rightIcon="../../../static/icon/my_module/healthInfo/jiantou.png"
					rightContentIcon="../../../static/icon/my_module/healthInfo/weixin.png"></listCell>
			</view>

		</view>
	</view>
</template>

<script>
	import bigBtnL from '@/components/bigButtonLeft.vue';
	import listCell from '@/components/listCell.vue';
	import avatar from "@/components/yq-avatar/yq-avatar.vue";
	export default {
		components: {
			bigBtnL,
			listCell,
			avatar
		},
		data() {
			return {
				src: '../../../static/icon/my_module/healthInfo/default.png',
				avatar: "",

				btnBoxWidthL: 300,
				btnBoxHeightL: 120,
				btnColorL: "#3FB8D9",
				btnImgSrcL: "../../../static/icon/training/share.png",
				btnTextL: "分享视频",
				btnTextColorL: "#ffffff",
				btnTextSizeL: 20,
				btnImgWidthL: 50,
				btnRadiusL: 50,

				btnBoxWidthR: 300,
				btnBoxHeightR: 120,
				btnColorR: "#FF6B3D",
				btnImgSrcR: "../../../static/icon/training/home.png",
				btnTextR: "返回首页",
				btnTextColorR: "#ffffff",
				btnTextSizeR: 20,
				btnImgWidthR: 50,
				btnRadiusR: 50,
				data: {}
			}
		},
		onLoad() {
			this.getInfo();
		},
		onNavigationBarButtonTap() {
			this.submit();
		},
		methods: {
			myUpload(rsp) {
				var me = this;
				me.avatar = rsp.path;
				this.submit_data()
			},
			//提交数据
			submit_data() {
				var me = this;
				uni.showLoading({
					title: "保存中"
				})
				// var str = JSON.stringify(me.formdata);
				var usertoken = uni.getStorageSync("usertoken");
				var info = uni.getStorageSync("accountInfo");
				uni.uploadFile({
					url: me.serverUrl + "/user/submitAvatar", //仅为示例，非真实的接口地址
					filePath: me.avatar,
					name: 'file',
					formData: {
						"openid": info.openid
					},
					header: {
						"usertoken": usertoken
					},
					success: (res) => {
						let data = JSON.parse(res.data);
						console.log(data)
						info.avatar = data.avatar;
						if (data.status == 200) {
							uni.setStorageSync("accountInfo", info);
							uni.showToast({
								title: "修改信息成功",
								duration: 1000
							})
							uni.navigateBack({
								delta: 1
							})
						}
					},
					fail: () => {
						uni.hideLoading();
					}
				});
			},
			submit() {
				var me = this;
				let token = uni.getStorageSync("usertoken");
				let data = uni.getStorageSync("accountInfo");

				data.gender=me.data.gender;
				uni.request({
					url: me.serverUrl + "/user/editAccountInfo",
					data: {
						"ipName": data.ipname,
						"name": data.name,
						"gender": data.gender,
						"address": data.address
					},
					header: {
						"usertoken": token
					},
					method: "POST",
					success(res) {
						console.log(res.data)
						if (res.data.status == 200) {
							console.log(res.data)
							uni.setStorageSync("accountInfo", data);
							uni.navigateBack({
								delta: 1
							})
							uni.showToast({
								title: "修改成功"
							})
						}
					},
					fail: () => {
						uni.showToast({
							duration: 800,
							title: "获取验证码失败"
						})
					}
				})
			},
			jumpEdit(index) {
				console.log(index);
				var me=this;
				if (index == 2) {
					uni.showActionSheet({
						itemList: ["男", "女"],
						success(res) {
							var Index = res.tapIndex;
							me.select = true;
							if (Index == 0) {
								me.data.gender = "男";
							} else if (Index == 1) {
								me.data.gender = "女";
							}
						}
					});
				} else {
					uni.navigateTo({
						url: './editAccouunt/editAccouunt?index=' + index
					})
				}
			},
			getInfo() {
				this.data = uni.getStorageSync("accountInfo");
				console.log(this.data)
				this.avatar = this.data.avatar;
			}
		}
	}
</script>

<style>
	@import url("account.css");
</style>
